import example.api.ShowDate;
import example.api.ShowDateByChina;
import example.api.ShowDateByEnglish;
import framework.config.XServiceConfig;
import framework.proxy.ClientProxy;
import framework.registry.Registry;
import framework.registry.ZookeeperRegistry;
import framework.transport.client.ChannelProvider;
import framework.transport.client.NettyClient;
import framework.transport.cluster.Cluster;
import framework.transport.cluster.FailFastCluster;
import framework.transport.loadbalance.LoadBalance;
import framework.transport.loadbalance.RandomLoadBalance;

public class Client {

    public static void main(String[] args) throws Exception {

        // 1：创建注册中心
        Registry registry = new ZookeeperRegistry("127.0.0.1:2181");
        // 2：创建通道提供者
        ChannelProvider channelProvider = new ChannelProvider();
        // 2.1创建负载均衡器
        LoadBalance loadBalance = new RandomLoadBalance();
        // 3：创建netty客户端
        NettyClient nettyClient = new NettyClient(registry, loadBalance, channelProvider);
        // 4：创建集群容错机制
        Cluster cluster = new FailFastCluster(nettyClient);
        // 5：创建方法的config
        XServiceConfig xServiceConfig1 = new XServiceConfig("3.1-cn", "group1", new ShowDateByChina());

        XServiceConfig xServiceConfig2 = new XServiceConfig("3.-en", "group1", new ShowDateByEnglish());
        System.out.println(new ClientProxy(xServiceConfig2, cluster).getProxy(ShowDate.class).show());

        // 6：创建方法的代理对象
        ClientProxy clientProxy = new ClientProxy(xServiceConfig1, cluster);
        ShowDate proxy = clientProxy.getProxy(ShowDate.class);
        // 7：执行方法
        String show = proxy.show();
        System.out.println(show);
    }


}
